﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Agents.Data;

namespace Agents.Admin
{
    public partial class ProductStatistics : System.Web.UI.Page
    {
        #region Event Handlers

        /// <summary>
        /// Occurs when the page is loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            // Initial binding
            if (!IsPostBack)
            {
                // Default is most selling products
                Queries queries = new Queries();
                dgProducts.DataSource = 
                    queries.GetTopFiveSellingProducts(TopProductsCreteria.NUM_OF_ORDERS);
                dgProducts.DataBind();

                panelTopProducts.Visible = radTopProducts.Checked;
                panelSalesByProduct.Visible = radSalesByProduct.Checked;
            }
        }

        /// <summary>
        /// Redirects the admin back to his homepage when the "back" button is pressed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void backBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/AdminMenu.aspx");
        }

        /// <summary>
        /// Occurs when the creteria/ Report type changes and re-binds the results
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void rad_CheckedChanged(object sender, EventArgs e)
        {
            Queries queries = new Queries();
            TopProductsCreteria creteria; 
            
            // Determine the creteria from the radio buttons
            if (radCreteriaItems.Checked)
            {
                creteria = TopProductsCreteria.SUM_ITEMS;
            }
            else if (radCreteriaOrders.Checked)
            {
                creteria = TopProductsCreteria.NUM_OF_ORDERS;
            }
            else
            {
                creteria = TopProductsCreteria.MOST_RECENTLY_ORDERED;
            }
            
            // Update the binding
            if (radMostSelling.Checked)
            {
                dgProducts.DataSource = queries.GetTopFiveSellingProducts(creteria);
            }
            else if (radLeastSelling.Checked)
            {
                dgProducts.DataSource = queries.GetFiveLeastSellingProducts(creteria);
            }

            dgProducts.DataBind();
        }

        /// <summary>
        /// Occurs when a product is bound and updates its category name from id
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dgProducts_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || 
                (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                // Retrieve the category Id
                int categoryId = Convert.ToInt32(e.Item.Cells[3].Text);

                // Display the category name in the 4th cell
                Queries queries = new Queries();
                e.Item.Cells[4].Text = queries.GetCategory(categoryId).Name;
            }
        }

        /// <summary>
        /// Occurs when the selected products changes and binds the chart
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmbProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Show months in last year
            if (radTimeResolution.SelectedValue == "YearByMonths")
            {
                BuildProductsSalesGraph(TimeResolution.MONTHS_IN_YEAR);
            }
            // Show days in last month
            else if (radTimeResolution.SelectedValue == "MonthByDays")
            {
                BuildProductsSalesGraph(TimeResolution.DAYS_IN_MONTH);
            }
        }

        /// <summary>
        /// Occurs when the report changes and re-binds the results
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void radReport_CheckedChanged(object sender, EventArgs e)
        {
            panelTopProducts.Visible = radTopProducts.Checked;
            panelSalesByProduct.Visible = radSalesByProduct.Checked;
        }

        /// <summary>
        /// Change the time resoultion of the graph
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void radTimeResolution_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Show months in last year
            if (radTimeResolution.SelectedValue == "YearByMonths")
            {
                BuildProductsSalesGraph(TimeResolution.MONTHS_IN_YEAR);
            }
            // Show days in last month
            else if (radTimeResolution.SelectedValue == "MonthByDays")
            {
                BuildProductsSalesGraph(TimeResolution.DAYS_IN_MONTH);
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Prepare the graph of products sales
        /// </summary>
        private void BuildProductsSalesGraph(TimeResolution timeResolution)
        {
            Queries queries = new Queries();

            // Get the selected product
            Product selectedProduct = queries.GetProduct(Int32.Parse(
                cmbProducts.SelectedValue));

            // Create the X axis 
            Dictionary<string, int> sales;

            // Months in last year
            if (timeResolution == TimeResolution.MONTHS_IN_YEAR)
            {
                sales = queries.GetProductSellsLastYear(selectedProduct.Id);
            }
            else
            {
                sales = queries.GetProductSellsLastMonth(selectedProduct.Id);
            }

            productSellsChart.Series[0].Points.DataBindXY(sales, "key", sales, "value");
        }

        #endregion
    }
}